using System;
using System.Globalization;
using System.Windows.Forms;
using Accounting.Model;

namespace EIM_Accounting
{
    /// <summary>
    /// 合同 合同管理 合同实际时间编辑
    /// </summary>
    public partial class CP010105 : Form
    {
        public string contractID = string.Empty;

        BLL.CP010105 cp010105 = new BLL.CP010105();

        public CP010105()
        {
            InitializeComponent();
        }

        private void CP010105_Load(object sender, System.EventArgs e)
        {
            if (contractID == string.Empty)
            {
                this.Close();
            }
            Contract contract = cp010105.GetModel(contractID);
            if (contract.RealStartDate == null || contract.RealStartDate == string.Empty)
            {
                this.dateTimePicker1.CustomFormat = "   ";
            }
            else
            {
                try
                {
                    dateTimePicker1.Value = DateTime.ParseExact(contract.RealStartDate, "yyyyMMdd", CultureInfo.InvariantCulture);
                }
                catch
                {
                    this.dateTimePicker1.CustomFormat = "   ";
                }
            }

            if (contract.RealEndDate == null || contract.RealEndDate == string.Empty)
            {
                this.dateTimePicker2.CustomFormat = "   ";
            }
            else
            {
                try
                {
                    dateTimePicker2.Value = DateTime.ParseExact(contract.RealEndDate, "yyyyMMdd", CultureInfo.InvariantCulture);
                }
                catch
                {
                    this.dateTimePicker2.CustomFormat = "   ";
                }
            }
        }

        private void dateTimePicker_ValueChanged(object sender, System.EventArgs e)
        {
            ((DateTimePicker)sender).CustomFormat = "yyyy 年 MM 月 dd 日";
        }

        private void dateTimePicker_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Back)
            {
                ((DateTimePicker)sender).CustomFormat = "   ";
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDiscard_Click(object sender, EventArgs e)
        {
            Contract contract = new Contract();
            contract.ID = int.Parse(contractID);
            contract.FinishFlag = "2";//废弃
            cp010105.DiscardModel(contract);
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Contract contract = new Contract();
            contract.ID = int.Parse(contractID);
            if (dateTimePicker2.CustomFormat != "   ")
            {
                contract.RealEndDate = dateTimePicker2.Value.ToString("yyyyMMdd");
                contract.FinishFlag = "1";//关闭
            }
            else
            {
                contract.FinishFlag = "0";//未关闭
                contract.RealEndDate = null;
            }
            if (dateTimePicker1.CustomFormat != "   ")
            {
                contract.RealStartDate = dateTimePicker1.Value.ToString("yyyyMMdd");
            }
            cp010105.UpdateRealDate(contract);

            this.Close();
        }
    }
}